using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Terra.Core.Common.ApiResponse;
using Terra.Core.Common.Extensions;
using Terra.Core.Security;
using Terra.Core.Security.Permissions;
using Terra.Data.Models.Identity;
using Terra.Service.Identity;
using Terra.Service.Identity.DTOs;
using Terra.Service.Identity.Interfaces;

namespace Terra.WebApi.Controllers.Identity;

/// <summary>
/// 权限控制器
/// </summary>
[Area("Admin")]
[ApiController]
[Route("api/admin/[controller]")]
[ApiExplorerSettings(GroupName = "v1-admin")]
[Authorize]
public class PermissionController : ControllerBase
{
    private readonly IPermissionService _permissionService;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="permissionService">权限服务</param>
    public PermissionController(IPermissionService permissionService)
    {
        _permissionService = permissionService;
    }

    /// <summary>
    /// 获取用户菜单列表
    /// </summary>
    /// <returns>菜单列表</returns>
    [HttpGet("menus")]
    [Permission("system:menu:list")]
    public async Task<ApiResponse<List<MenuTreeDto>>> GetUserMenusAsync()
    {
        var userId = User.GetUserId();
        var menus = await _permissionService.GetUserMenusAsync(userId.Value);
        return menus;
    }

    /// <summary>
    /// 获取用户权限码列表
    /// </summary>
    /// <returns>权限码列表</returns>
    [HttpGet("codes")]
    [Permission("system:permission:list")]
    public async Task<ApiResponse<List<string>>> GetUserPermissionCodesAsync()
    {
        var userId = User.GetUserId();
        var codes = await _permissionService.GetUserPermissionCodesAsync(userId.Value);
        return codes;
    }
}